package com.zxl.yjb.fragment.home;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.zxl.yjb.R;
import com.zxl.yjb.activity.home.SkillRegisterActivity;
import com.zxl.yjb.activity.home.order.OrderCreateActivity;
import com.zxl.yjb.activity.home.order.ServiceSelectActivity;
import com.zxl.yjb.adapter.SkillAdapter;
import com.zxl.yjb.base.LazyBaseFragment;
import com.zxl.yjb.bean.SkillClassifyBean;
import com.zxl.yjb.utils.DisplayUtil;
import com.zxl.yjb.widget.PagerRecyclerView.DividerGridItemDecoration;

import java.util.ArrayList;

import butterknife.BindView;
import butterknife.ButterKnife;

public class SkillFragment extends LazyBaseFragment {

    @BindView(R.id.rv_skill)
    RecyclerView rv_skill;

    private SkillAdapter skillAdapter;
    private GridLayoutManager gridLayoutManager;
    private DividerGridItemDecoration dividerGridItemDecoration;

    private ArrayList<SkillClassifyBean.SkillBean> skillBeans;

    private SkillRegisterActivity avtivity;

    private String origin = "";
    private boolean isSelectMode;


    @Override
    protected int getLayoutId() {
        return R.layout.fragment_skill;
    }


    public void setData(ArrayList<SkillClassifyBean.SkillBean> skillBeans) {
        this.skillBeans = skillBeans;
    }

    public void setOrigin(String origin)
    {
        this.origin = origin;
    }

    @Override
    protected void initView(View view, Bundle savedInstanceState) {
        ButterKnife.bind(this, view);
        if (origin.equals(ServiceSelectActivity.class.getName())||origin.equals(HomeFragment.class.getName()))//如果是发单功能跳转到这里,或者首页父级类型跳到这里
        {
            isSelectMode = false;
        }
        else
        {
            isSelectMode = true;
            avtivity = (SkillRegisterActivity) mActivity;
        }

        gridLayoutManager = new GridLayoutManager(getActivity(),3);
        rv_skill.setLayoutManager(gridLayoutManager);
        dividerGridItemDecoration = new DividerGridItemDecoration(getActivity(), DisplayUtil.dip2px(getActivity(),5),R.color.tab_color);
        rv_skill.addItemDecoration(dividerGridItemDecoration);
        skillAdapter = new SkillAdapter(R.layout.item_skill,skillBeans,isSelectMode);
        rv_skill.setAdapter(skillAdapter);

        skillAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {

                if (origin.equals(ServiceSelectActivity.class.getName()))//如果是发单功能跳转到这里
                {
                    Intent intent = new Intent(getActivity(),ServiceSelectActivity.class);
                    intent.putExtra("id",skillBeans.get(position).getId());
                    intent.putExtra("pid",skillBeans.get(position).getPid());
                    intent.putExtra("name",skillBeans.get(position).getName());
                    getActivity().setResult(Activity.RESULT_OK,intent);
                    getActivity().finish();
                }
                else if (origin.equals(HomeFragment.class.getName()))//如果是首页父级类型跳到这里
                {
                    Intent intent = new Intent(getActivity(),OrderCreateActivity.class);
                    intent.putExtra("id",skillBeans.get(position).getId());
                    intent.putExtra("pid",skillBeans.get(position).getPid());
                    intent.putExtra("name",skillBeans.get(position).getName());
                    getActivity().startActivity(intent);
                    getActivity().finish();
                }
                else
                {
                    if (skillBeans.get(position).isSelect()||skillBeans.get(position).getIsSkillReg() == 1) {
                        skillBeans.get(position).setSelect(false);
                        skillBeans.get(position).setIsSkillReg(0);
                        removeSkill(skillBeans.get(position).getId());
                    } else {
                        skillBeans.get(position).setSelect(true);
                        skillBeans.get(position).setIsSkillReg(1);
                        avtivity.submitSkillList.add(skillBeans.get(position));
                        avtivity.submitSkillAdapter.notifyDataSetChanged();
                    }

                    skillAdapter.notifyDataSetChanged();
                }
            }
        });

    }



    private void removeSkill(String id)
    {
        for (int i = 0; i < avtivity.submitSkillList.size(); i++) {
            if (avtivity.submitSkillList.get(i).getId().equals(id)) {
                avtivity.submitSkillList.remove(i);
                avtivity.submitSkillAdapter.notifyDataSetChanged();
            }
        }
    }

}
